import scrapy
from bossPro.items import BossproItem

class BossSpider(scrapy.Spider):
    name = "boss"
    # allowed_domains = ["www.xxx.com"]
    start_urls = ["https://www.zhipin.com/job_detail/0780cf9d678f703f1Hd-3d-9GFdY.html"]

    # 解析详情页的岗位信息
    def parse_detail(self,response):
        print('*************开始解析URL详情*************')
        item = response.meta['item']
        job_desc = response.xpath('//div[class="job-sec-text"]/text()').extract()
        job_desc = ''.join(job_desc)
        item['job_desc'] = job_desc

        yield item

    # 解析首页的岗位名称
    def parse(self, response):
        print('开始进入爬虫...')
        li_lst = response.xpath('//*[@id="main"]/div/div[4]/ul/li[8]/ul/li')
        # print(li_lst)
        for li in li_lst:
            item = BossproItem()
            detail_url = 'https://www.zhipin.com' + li.xpath('./a/@href').extract()[0]
            job_name =  li.xpath('./a/div[1]/p/text()').extract()[0]
            job_address = li.xpath('./a/p/span/text()').extract()[0]
            item['job_name'] = job_name
            print(detail_url,'   ',job_name,'   ',job_address)
            # 对详情页发请求获取详情页的页面源码数据
            # 手动请求的发送
            # 请求传参: meta={},可以将meta字典传递给请求对应的回调函数
            yield scrapy.Request(url=detail_url,callback=self.parse_detail,meta={'item':item})